﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class SysIpBlock
    {
        public int Add(KeRui.Model.SysIpBlock model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@IpStart", SqlDbType.BigInt, 8), new SqlParameter("@IpEnd", SqlDbType.BigInt, 8), new SqlParameter("@Name", SqlDbType.NVarChar, 50), new SqlParameter("@BlockType", SqlDbType.Int, 4), new SqlParameter("@BlockModule", SqlDbType.Int, 4) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.IpStart;
            parameters[2].Value = model.IpEnd;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.BlockType;
            parameters[5].Value = model.BlockModule;
            DbHelperSQL.RunProcedure("SysIpBlock_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public void Delete(int ID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            DbHelperSQL.RunProcedure("SysIpBlock_Delete", parameters, out num);
        }

        public KeRui.Model.SysIpBlock GetModel(int ID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            KeRui.Model.SysIpBlock block = new KeRui.Model.SysIpBlock();
            DataSet set = DbHelperSQL.RunProcedure("SysIpBlock_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    block.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
                }
                if (set.Tables[0].Rows[0]["IpStart"].ToString() != "")
                {
                    block.IpStart = long.Parse(set.Tables[0].Rows[0]["IpStart"].ToString());
                }
                if (set.Tables[0].Rows[0]["IpEnd"].ToString() != "")
                {
                    block.IpEnd = long.Parse(set.Tables[0].Rows[0]["IpEnd"].ToString());
                }
                block.Name = set.Tables[0].Rows[0]["Name"].ToString();
                if (set.Tables[0].Rows[0]["BlockType"].ToString() != "")
                {
                    block.BlockType = int.Parse(set.Tables[0].Rows[0]["BlockType"].ToString());
                }
                if (set.Tables[0].Rows[0]["BlockModule"].ToString() != "")
                {
                    block.BlockModule = int.Parse(set.Tables[0].Rows[0]["BlockModule"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    block.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return block;
            }
            return null;
        }

        public void Update(KeRui.Model.SysIpBlock model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@IpStart", SqlDbType.BigInt, 8), new SqlParameter("@IpEnd", SqlDbType.BigInt, 8), new SqlParameter("@Name", SqlDbType.NVarChar, 50), new SqlParameter("@BlockType", SqlDbType.Int, 4), new SqlParameter("@BlockModule", SqlDbType.Int, 4) };
            parameters[0].Value = model.ID;
            parameters[1].Value = model.IpStart;
            parameters[2].Value = model.IpEnd;
            parameters[3].Value = model.Name;
            parameters[4].Value = model.BlockType;
            parameters[5].Value = model.BlockModule;
            DbHelperSQL.RunProcedure("SysIpBlock_Update", parameters, out num);
        }
    }
}

